package com.stylefeng.guns.modular.poemspoetry.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.poemspoetry.model.PoemsAuthor;
import com.stylefeng.guns.modular.poemspoetry.service.IPoemsAuthorService;

/**
 * 诗人控制器
 *
 * @author fengshuonan
 * @Date 2018-03-20 15:57:40
 */
@Controller
@RequestMapping("/poemsAuthor")
public class PoemsAuthorController extends BaseController {

    private String PREFIX = "/poemspoetry/poemsAuthor/";

    @Autowired
    private IPoemsAuthorService poemsAuthorService;

    /**
     * 跳转到诗人首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "poemsAuthor.html";
    }

    /**
     * 跳转到添加诗人
     */
    @RequestMapping("/poemsAuthor_add")
    public String poemsAuthorAdd() {
        return PREFIX + "poemsAuthor_add.html";
    }

    /**
     * 跳转到修改诗人
     */
    @RequestMapping("/poemsAuthor_update/{poemsAuthorId}")
    public String poemsAuthorUpdate(@PathVariable Long poemsAuthorId, Model model) {
        PoemsAuthor poemsAuthor = poemsAuthorService.selectById(poemsAuthorId);
        model.addAttribute("item",poemsAuthor);
        LogObjectHolder.me().set(poemsAuthor);
        return PREFIX + "poemsAuthor_edit.html";
    }

    /**
     * 获取诗人列表
     */
    /*
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return poemsAuthorService.selectList(null);
    }
    */

     /**
     * 获取诗人列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        Page<PoemsAuthor> page = new PageFactory<PoemsAuthor>().defaultPage();
        EntityWrapper<PoemsAuthor> ew = new EntityWrapper<PoemsAuthor>();
         /*if (ToolUtil.isNotEmpty(condition)) {
                ew.eq("condition", condition);
         }*/
        return super.packForBT(poemsAuthorService.selectPage(page,ew));
    }

    /**
     * 新增诗人
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PoemsAuthor poemsAuthor) {
        poemsAuthorService.insert(poemsAuthor);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除诗人
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long poemsAuthorId) {
        poemsAuthorService.deleteById(poemsAuthorId);
        return SUCCESS_TIP;
    }

    /**
     * 修改诗人
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PoemsAuthor poemsAuthor) {
        poemsAuthorService.updateById(poemsAuthor);
        return super.SUCCESS_TIP;
    }

    /**
     * 诗人详情
     */
    @RequestMapping(value = "/detail/{poemsAuthorId}")
    @ResponseBody
    public Object detail(@PathVariable("poemsAuthorId") Long poemsAuthorId) {
        return poemsAuthorService.selectById(poemsAuthorId);
    }
}
